﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.ServiceModel.DomainServices.Client;
using ZL.Push.Web;


namespace ZL.Push
{
	public partial class FriendForm2 : UserControl, INotifyPropertyChanged
	{
        #region INotifyPropertyChanged 成员
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private tbl_friend _friend;
        public tbl_friend friend
        {
            get
            {
                return _friend;
            }
            set
            {
                if (_friend != value)
                {
                    _friend = value;
                    this.NotifyPropertyChanged("friend");
                }
            }
        }

        // 从friend获得user，从而显示更多好友信息
        private tbl_user _user;
        public tbl_user user
        {
            get { return _user; }
            set
            {
                if (_user != value)
                {
                    _user = value;
                    this.NotifyPropertyChanged("user");
                }
            }
        }

        public FriendForm2()
		{
			InitializeComponent();

            _control_detail.DataContext = this;
            _control_metadata._search_control.Visibility = System.Windows.Visibility.Collapsed;

            _tabcontrol.SelectionChanged += new SelectionChangedEventHandler(_tabcontrol_SelectionChanged);
        }

        void _tabcontrol_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _update_ui();
        }

        public void update_ui(tbl_friend friend_)
        {
            friend = friend_;
            _update_ui();

            if (friend == null)
            {
                user = null;
            }
            else
            {
                _get_user_from_web();
            }
        }

        protected void _update_ui()
        {
            if (_tabitem_metadata.IsSelected)
            {
                if (friend != null)
                {
                    _control_metadata.search(friend.friend_id);
                }
                else
                {
                    _control_metadata.clear();
                }
            }
        }

        protected void _get_user_from_web()
        {
            App app = App.Current as App;
            EntityQuery<tbl_user> query = app.context.GetTbl_userQuery().Where(c => c.id == friend.friend_id);
            app.context.Load(query, LoadBehavior.RefreshCurrent, (lo) => // 2013.1.1 modify by long 添加RefreshCurrent以避免好友更改头像不刷新
            {
                if (lo.HasError)
                {
                    user = null;
                    MessageBox.Show(lo.Error.Message);
                }
                else if (lo.Entities.Count() > 0)
                {
                    user = lo.Entities.ElementAt(0);

                    // 在此刷新has_image
                    if (friend.has_image != user.has_image)
                    {
                        friend.has_image = user.has_image;

                        app.context.SubmitChanges((op) =>
                        {
                            // app.get_main_page()._create_friend_tree();
                        }, false);
                    }
                }
                else
                {
                    user = null;
                }
            }, null);
        }
    }
}
